﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Biblioteca.DataBase;
using Pms.Utils;
using SERIX.Util;
using System.Data.Common;
using Biblioteca.Objetos;

namespace SERIX.Controles
{
    public partial class lovBeneficiarios : System.Web.UI.UserControl
    {

        string _sessionId;

        public delegate void SelecaoDelegate(string chave, string descricao);
        public event SelecaoDelegate Selecao;
        protected void Page_Load(object sender, EventArgs e)
        {
            _sessionId = "pagAtual" + this.ID;
            Navegador1.SessionId = _sessionId = "pagAtual" + this.ID;
            Navegador1.Pesquisar += new SERIX.Controles.Navegador.PesquisarDelegate(Pesquisar);
            if (!Page.IsPostBack)
            {
                Session[_sessionId] = 1;
            }
        }

        protected void btnLov_Click(object sender, EventArgs e)
        {

        }

        protected void btnPequisar_Click(object sender, EventArgs e)
        {
            Pesquisar();
            Panel1_ModalPopupExtender.Show();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Panel1_ModalPopupExtender.Hide();
        }

        protected void Pesquisar()
        {
            
            string _campo = "";
            QueryPaginada qy = null;

            string filtro = "";

            switch (ddlPor.SelectedValue)
            {
                case "N": _campo = "NOM_BENEF"; break;
                case "C": _campo = "CPF"; break;
                case "R": _campo = "RG"; break;
            }
            switch (RadioButtonList1.SelectedValue)
            {
                case "E": filtro = "UPPER(:" + _campo + ")  || '%'"; break;
                case "D": filtro = "'%' || UPPER(:" + _campo + ")"; break;
                case "A": filtro = "'%' || UPPER(:" + _campo + ") || '%'"; break;
            }

            string SQL = @" SELECT idt_benef, cpf, rg, nom_benef " +
                          "   FROM serix.srx_beneficiarios " +
                          " WHERE UPPER(" + _campo + ") LIKE " + filtro +
                          " order by nom_benef";
            try
            {
                Usuario usuAut = (Usuario)Session["usuario"];
                qy = new QueryPaginada(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, usuAut.Login, BiblioPMS.Demo), SQL, BiblioSERIX.tamPagLov);
                qy.AdicionarParametro(_campo, DbType.String, txtFiltrar.Text);
                gridView.DataSource = qy.Executar(Convert.ToInt32(Session[_sessionId]));
                gridView.DataBind();
                Navegador1.CarregueBotoes(qy.QtdLinhas());
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                    qy.FecharConexao();
            }
            finally
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                    qy.FecharConexao();
            }
            Panel1_ModalPopupExtender.Show();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string chave = ((LinkButton)sender).Attributes["chave"];
            string nome = ((LinkButton)sender).Attributes["nome"];
            Selecao(chave, nome);
            Panel1_ModalPopupExtender.Hide();
        }

        protected void gridView_RowDataBound1(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string IDT = ((DbDataRecord)e.Row.DataItem)["Idt_Benef"].ToString();
                string NOME = ((DbDataRecord)e.Row.DataItem)["NOM_BENEF"].ToString();
                string CPF = ((DbDataRecord)e.Row.DataItem)["CPF"].ToString();
                string RG = ((DbDataRecord)e.Row.DataItem)["RG"].ToString();

                ((LinkButton)e.Row.Cells[gridView.Columns.Count - 3].Controls[1]).Attributes.Add("chave", IDT);
                ((LinkButton)e.Row.Cells[gridView.Columns.Count - 3].Controls[1]).Attributes.Add("nome", NOME);
                ((LinkButton)e.Row.Cells[gridView.Columns.Count - 3].Controls[1]).Text = CPF;
                ((LinkButton)e.Row.Cells[gridView.Columns.Count - 2].Controls[1]).Attributes.Add("chave", IDT);
                ((LinkButton)e.Row.Cells[gridView.Columns.Count - 2].Controls[1]).Attributes.Add("nome", NOME);
                ((LinkButton)e.Row.Cells[gridView.Columns.Count - 2].Controls[1]).Text = RG;
                ((LinkButton)e.Row.Cells[gridView.Columns.Count - 1].Controls[1]).Attributes.Add("chave", IDT);
                ((LinkButton)e.Row.Cells[gridView.Columns.Count - 1].Controls[1]).Attributes.Add("nome", NOME);
                ((LinkButton)e.Row.Cells[gridView.Columns.Count - 1].Controls[1]).Text = NOME;
            }
        }

       

    }
}